home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / ULARN.ARJ / ULARN.TAR / ularn / signal.c < prev    next >
C/C++ Source or Header  |  1989-10-25  |  5KB  |  178 lines

  1. /* signal.c */
  2.  
  3. #include "header.h"            
  4.  
  5. #define BIT(a) (1<<((a)-1))
  6. extern char savefilename[],wizard,predostuff,nosignal;
  7.  
  8. s2choose()    /* text to be displayed if ^C during intro screen */
  9. {
  10.     cursor(1,24); 
  11.     lprcat("Press "); 
  12.     if (boldon) setbold(); 
  13.     lprcat("return"); 
  14.     if (boldon) resetbold();
  15.     lprcat(" to continue: ");
  16.     lflush(); 
  17. }
  18.  
  19. cntlc()    /* what to do for a ^C */
  20. {
  21.     if (nosignal) 
  22.         return;    /* don't do anything if inhibited */
  23.     signal(SIGINT,SIG_IGN);
  24.     quit(); 
  25.     if (predostuff==1) 
  26.         s2choose(); 
  27.     else 
  28.         showplayer();
  29.     lflush();
  30.     signal(SIGQUIT,cntlc);    
  31.     signal(SIGINT,cntlc);
  32. }
  33.  
  34. /*
  35.  *    subroutine to save the game if a hangup signal
  36.  */
  37. sgam()
  38. {
  39.     savegame(savefilename);  
  40.     wizard=1;  
  41.     died(-257); /* hangup signal */
  42. }
  43.  
  44. #ifdef SIGTSTP
  45. tstop() 
  46. {
  47.     if (nosignal)  
  48.         return;  /* nothing if inhibited */
  49.     lcreat((char*)0);  
  50.     clearvt100();    
  51.     lflush();      
  52.     signal(SIGTSTP,SIG_DFL);
  53. #ifdef SIGVTALRM
  54. /* looks like BSD4.2 or higher - must clr mask for signal to take effect*/
  55.     sigsetmask(sigblock(0)& ~BIT(SIGTSTP));
  56. #endif
  57.     kill(getpid(),SIGTSTP);
  58.  
  59.     setupvt100();  
  60.     signal(SIGTSTP,tstop);
  61.     if (predostuff==1) 
  62.         s2choose(); 
  63.     else 
  64.         drawscreen();
  65.     showplayer();    
  66.     lflush();
  67. }
  68. #endif /* SIGTSTP */
  69.  
  70. /*
  71.  *    subroutine to issue the needed signal traps  called from main()
  72.  */
  73. sigill()  { sigpanic(SIGILL); }     
  74. sigtrap() { sigpanic(SIGTRAP); }
  75. sigiot()  { sigpanic(SIGIOT); }   
  76. sigemt()  { sigpanic(SIGEMT); }
  77. sigfpe()  { sigpanic(SIGFPE); }   
  78. sigbus()  { sigpanic(SIGBUS); }
  79. sigsegv() { sigpanic(SIGSEGV); }  
  80. sigsys()  { sigpanic(SIGSYS); }
  81. sigpipe() { sigpanic(SIGPIPE); }  
  82. sigterm() { sigpanic(SIGTERM); }
  83.  
  84. sigsetup()
  85. {
  86.     signal(SIGQUIT, cntlc);         
  87.     signal(SIGINT,  cntlc); 
  88.     signal(SIGKILL, SIG_IGN);        
  89.     signal(SIGHUP,  sgam);
  90.     signal(SIGILL,  sigill);        
  91.     signal(SIGTRAP, sigtrap);
  92.     signal(SIGIOT,  sigiot);        
  93.     signal(SIGEMT,  sigemt);
  94.     signal(SIGFPE,  sigfpe);        
  95.     signal(SIGBUS,  sigbus);
  96.     signal(SIGSEGV, sigsegv);        
  97.     signal(SIGSYS,  sigsys);
  98.     signal(SIGPIPE, sigpipe);        
  99.     signal(SIGTERM, sigterm);
  100. #ifdef SIGTSTP
  101.     signal(SIGTSTP,tstop);        
  102.     signal(SIGSTOP,tstop);
  103. #endif /* SIGTSTP */
  104. }
  105.  
  106. #ifdef BSD    /* for BSD UNIX? */
  107.  
  108. static char *signame[NSIG] = { 
  109.     "",
  110.     "SIGHUP",  /*    1     hangup */
  111.     "SIGINT",  /*    2     interrupt */
  112.     "SIGQUIT", /*    3     quit */
  113.     "SIGILL",  /*    4     illegal instruction (not reset when caught) */
  114.     "SIGTRAP", /*    5     trace trap (not reset when caught) */
  115.     "SIGIOT",  /*    6     IOT instruction */
  116.     "SIGEMT",  /*    7     EMT instruction */
  117.     "SIGFPE",  /*    8     floating point exception */
  118.     "SIGKILL", /*    9     kill (cannot be caught or ignored) */
  119.     "SIGBUS",  /*    10     bus error */
  120.     "SIGSEGV", /*    11     segmentation violation */
  121.     "SIGSYS",  /*    12     bad argument to system call */
  122.     "SIGPIPE", /*    13     write on a pipe with no one to read it */
  123.     "SIGALRM", /*    14     alarm clock */
  124.     "SIGTERM", /*    15     software termination signal from kill */
  125.     "SIGURG",  /*    16     urgent condition on IO channel */
  126.     "SIGSTOP", /*    17     sendable stop signal not from tty */
  127.     "SIGTSTP", /*    18     stop signal from tty */
  128.     "SIGCONT", /*    19     continue a stopped process */
  129.     "SIGCHLD", /*    20     to parent on child stop or exit */
  130.     "SIGTTIN", /*    21     to readers pgrp upon background tty read */
  131.     "SIGTTOU", /*    22     like TTIN for output if (tp->t_local<OSTOP)*/
  132.     "SIGIO",   /*    23     input/output possible signal */
  133.     "SIGXCPU", /*    24     exceeded CPU time limit */
  134.     "SIGXFSZ", /*    25     exceeded file size limit */
  135.     "SIGVTALRM",/*  26     virtual time alarm */
  136.     "SIGPROF", /*    27     profiling time alarm */
  137.     "","","","" };
  138.  
  139. #else  /* SYSV */
  140. static char *signame[NSIG] = { 
  141.     "",
  142.     "SIGHUP",  /*    1     hangup */
  143.     "SIGINT",  /*    2     interrupt */
  144.     "SIGQUIT", /*    3     quit */
  145.     "SIGILL",  /*    4     illegal instruction (not reset when caught) */
  146.     "SIGTRAP", /*    5     trace trap (not reset when caught) */
  147.     "SIGIOT",  /*    6     IOT instruction */
  148.     "SIGEMT",  /*    7     EMT instruction */
  149.     "SIGFPE",  /*    8     floating point exception */
  150.     "SIGKILL", /*    9     kill (cannot be caught or ignored) */
  151.     "SIGBUS",  /*    10     bus error */
  152.     "SIGSEGV", /*    11     segmentation violation */
  153.     "SIGSYS",  /*    12     bad argument to system call */
  154.     "SIGPIPE", /*    13     write on a pipe with no one to read it */
  155.     "SIGALRM", /*    14     alarm clock */
  156.     "SIGTERM", /*    15     software termination signal from kill */
  157.     "SIGUSR1",  /*    16     user defines signal 1 */
  158.     "SIGUSR2", /*    17     user defines signal 2 */
  159.     "SIGCLD",  /*    18     child death */
  160.     "SIGPWR"   /*    19     power fail */
  161. };
  162. #endif /* BSD */
  163.  
  164. /*
  165.  *    routine to process a fatal error signal
  166.  */
  167. sigpanic(sig)
  168. int sig;
  169. {
  170.     signal(sig,SIG_DFL);
  171.     fprintf(stderr,"\nUlarn - SHIT! Signal %d !!! [%s]\n",sig,signame[sig]);
  172.     fflush(stderr);
  173.     sleep(2);
  174.     sncbr();
  175.     savegame(savefilename); 
  176.     kill(getpid(),sig); /* this will terminate us */
  177. }
  178.